<?php
/**
 * Description of FormIncludeViewGenerator
 *
 * @author Magno
 */
class FormIncludeViewGenerator extends ViewGenerator{

    public function __construct($tableMeta, $viewParams) {
        parent::__construct($tableMeta, $viewParams, Constantes::$TEMPLATES_VIEW_DIR."formInclude.bloum");
    }

    public function generate() {
        parent::generateNames();
        $this->generatePkValues();
        $this->generateAttValues();
        return $this->templateView;
    }

    public function generatePkValues(){

        $tableName = $this->tableMeta->getName();
        $pkInputHiden = "";
        $inputString = '<input type="hidden" name="'.$tableName.'_:pk" id="'.$tableName.'_:pk" value="{$'.$tableName.'_:pk|default:0}" />'."\n\t\t\t";
        $attrsKeys = array_keys($this->attPK);

        for ($i = 0; $i < count($attrsKeys); $i++) {
            $att = $this->attPK[$attrsKeys[$i]];
            $name = $att->getName();
            $pkInputHiden .= str_replace(":pk", $name, $inputString);            
        }

        $this->templateView = str_replace(Constantes::$TPL_VIEW_CADASTRO_PK_INPUT_HIDDEN, $pkInputHiden, $this->templateView);

    }

    public function generateAttValues(){

        $tableName = $this->tableMeta->getName();
        $attributes = $this->tableMeta->getAttributes();
        $attrsKeys = array_keys($attributes);

        $attInputs = "";
        $fkInputs = "";

        for ($i = 0; $i < count($attrsKeys); $i++) {
            $att = $attributes[$attrsKeys[$i]];
            $name = $att->getName();

            if($att->isPrimaryKey() && !$att->isForeignKey()) continue;

            if(!$att->isForeignKey()){

                $required = "";
                if($att->isNotNull()){
                    $required = "required=true";
                }

                $attInputs .= '{form_input name="'.$tableName.'_'.$name.'" label="'.ucfirst($name).'" '.$required.' value="{$'.$tableName.'_'.$name.'|default:\'\'}"}';
                $attInputs .= "\n\t";
            }
            else{
                $tbFkName = str_replace("ID", "", $name);
                $fks = $this->tableMeta->getFKReferences();
                $fkMeta = null;
                foreach ($fks as $fk) {
                    foreach ($fk->getAttributes() as $fkAtt) {
                        if(strcmp(strtolower($fkAtt->getName()), strtolower($tbFkName."ID")) == 0){
                            $fkMeta = $fk;
                            break;
                        }
                    }
                    if($fkMeta != null) break;
                }
                if($fkMeta == null)
                    throw new Exception ("Tabela (".$tbFkName.") Referenciada Na Herança com (".$this->tableMeta->getName().") Não Encontrada!!!");

                $tbReference = $fkMeta->getTableReference()->getName();
                $tab = "\n";
                if(strcmp($fkMeta->getTypeForm(), Constantes::$TYPE_FORM_COMBO) == 0){
                    $fkInputs .= '<fieldset>'.$tab."\t".'<legend>Dados '.ucfirst($tbReference).'</legend>'.$tab."\t".'{include file="view/'.$tbReference.'/formCombo.tpl"}'.$tab.'</fieldset>';
                }/*else if(strcmp($fkMeta->getTypeForm(), Constantes::$TYPE_FORM_HIDDEN_ID) == 0){
                    $fkInputs .= '<input type="hidden" name="'.$tbReference.'" id="'.$tbReference.'" value="{$'.$tbReference.'ID|default:0}" />';
                }*/else if(strcmp($fkMeta->getTypeForm(), Constantes::$TYPE_FORM_INCLUDE) == 0){
                    $fkInputs .= '{include file="view/'.$tbReference.'/formInclude.tpl"}'.$tab;
                }
            }

        }

        $this->templateView = str_replace(Constantes::$TPL_VIEW_CADASTRO_ATT_INPUTS, $attInputs, $this->templateView);
        $this->templateView = str_replace(Constantes::$TPL_VIEW_CADASTRO_FK_INPUTS, $fkInputs, $this->templateView);

    }

    public function generateFile($generate = true){
        if($generate)
            $this->generate();
        parent::generateFile("formInclude");
    }
}
?>